<?php

namespace App\Jobs;

use App\Datalog\Events\LoginEventLog;
use App\Datalog\Users\LoginUserSet;
use App\Models\Log\LogLogin;
use App\Services\UserExtendService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class UserLoginQueue implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    use TraitQueue;

    protected $logSuffix;
    protected $logId;
    protected $params;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($log_suffix, $log_id, $params)
    {
        $this->logSuffix = $log_suffix;
        $this->logId     = $log_id;
        $this->params    = $params;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $log_login = LogLogin::suffix($this->logSuffix)->find($this->logId);

        // 数据落地
        LoginEventLog::getInstance()->addLog($log_login);
        LoginUserSet::getInstance()->addSet($log_login);

        // 更新扩展表
        UserExtendService::getInstance()->loginSuccess($log_login->user_id, $log_login->create_time);
    }
}
